<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AfterSales;
use App\Models\Orders;
use Illuminate\Http\Request;

class AfterSaleController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.orders.after_sale');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'state' => (int)$request->get('state',0),
                'type' => (int)$request->get('type',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
            ];
            $lists = AfterSales::getList($params);
//            dd($lists);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 详情
     * @param Request $request
     * @return bool|view
     */
    public function detail(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = AfterSales::findOrFail($id);
                //状态处理
                $info['type'] = $info['type'] == 1 ? '退款' : '退货';
                $info['user_message'] = $info['user_message'] == null ? '无' : $info['user_message'];
                $info['user_pic'] = $info['user_pic'] == null ? '无' : $info['user_pic'];
                $info['seller_message'] = $info['seller_message'] == null ? '无' : $info['seller_message'];
                if($info['state'] == 1){
                    $info['state'] = '审核中';
                }elseif ($info['state'] == 2){
                    $info['state'] = '同意';
                }elseif ($info['state'] == 3){
                    $info['state'] = '不同意';
                }
            }
//            dd($info->toArray());
            $info = json_encode($info);

            return view('admin.orders.after_sale_detail',compact('info','id'));

        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }


    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            AfterSales::destroy($id);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
